package com.carol.lmsbe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carol.lmsbe.dto.LaboratoryCreateRequest;
import com.carol.lmsbe.dto.LaboratoryDTO;
import com.carol.lmsbe.dto.LaboratoryQueryRequest;
import com.carol.lmsbe.dto.LaboratoryUpdateRequest;
import com.carol.lmsbe.common.Result;
import com.carol.lmsbe.service.LaboratoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 实验室管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/laboratories")
@RequiredArgsConstructor
@Validated
public class LaboratoryController {

    private final LaboratoryService laboratoryService;

    /**
     * 获取实验室列表（分页）
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<Page<LaboratoryDTO>> getLaboratoryList(LaboratoryQueryRequest request) {
        try {
            Page<LaboratoryDTO> page = laboratoryService.getLaboratoryList(request);
            return Result.success(page);
        } catch (Exception e) {
            log.error("获取实验室列表失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据ID获取实验室详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<LaboratoryDTO> getLaboratoryById(@PathVariable Long id) {
        try {
            LaboratoryDTO laboratory = laboratoryService.getLaboratoryById(id);
            return Result.success(laboratory);
        } catch (Exception e) {
            log.error("获取实验室详情失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建实验室
     */
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<LaboratoryDTO> createLaboratory(@Valid @RequestBody LaboratoryCreateRequest request) {
        try {
            LaboratoryDTO laboratory = laboratoryService.createLaboratory(request);
            return Result.success(laboratory);
        } catch (Exception e) {
            log.error("创建实验室失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新实验室
     */
    @PutMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<LaboratoryDTO> updateLaboratory(@Valid @RequestBody LaboratoryUpdateRequest request) {
        try {
            LaboratoryDTO laboratory = laboratoryService.updateLaboratory(request);
            return Result.success(laboratory);
        } catch (Exception e) {
            log.error("更新实验室失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除实验室
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> deleteLaboratory(@PathVariable Long id) {
        try {
            laboratoryService.deleteLaboratory(id);
            return Result.success();
        } catch (Exception e) {
            log.error("删除实验室失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取所有启用的实验室
     */
    @GetMapping("/active")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<List<LaboratoryDTO>> getAllActiveLaboratories() {
        try {
            List<LaboratoryDTO> laboratories = laboratoryService.getAllActiveLaboratories();
            return Result.success(laboratories);
        } catch (Exception e) {
            log.error("获取启用实验室列表失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}
